package com.googlecode.wheres_my_stuff_okay_potato;

import static android.widget.Toast.LENGTH_LONG;
import static android.widget.Toast.makeText;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;

@SuppressLint("DefaultLocale")
public class RegisterActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);

		final LoginHandler lh = new LoginHandler(getApplication()
				.getBaseContext());

		final EditText usernameET = (EditText) findViewById(R.id.username_et);
		final EditText passwordET = (EditText) findViewById(R.id.password_et);
		final EditText confirmET = (EditText) findViewById(R.id.confirm_password_et);
		final EditText emailET = (EditText) findViewById(R.id.email_et);

		Intent registerIntent = getIntent();
		String prevU = registerIntent.getExtras().get("username").toString();

		// Fill in the username box with whatever was entered on the login page,
		// if anything.
		usernameET.setText(prevU);

		usernameET.setOnFocusChangeListener(new OnFocusChangeListener() {
			public void onFocusChange(View v, boolean hasFocus) {
				if (lh.userExists(usernameET.getText().toString())) {
					makeText(getApplicationContext(),
							R.string.username_taken_toast, LENGTH_LONG).show();
					v.requestFocus();
				}
			}
		});

		// Register button
		findViewById(R.id.register_b).setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String username = usernameET.getText().toString();

				if (username.length() < 7) {
					makeText(getApplicationContext(),
							R.string.username_length_toast, LENGTH_LONG).show();
					usernameET.requestFocus();
					return;
				}

				// First validate the password:
				String password = passwordET.getText().toString();
				if (!password.matches("[\\da-zA-Z@#$%_-]{7,40}")) {
					makeText(getApplicationContext(),
							R.string.password_format_toast, LENGTH_LONG).show();
					passwordET.requestFocus();
					return;
				}

				if (!password.equals(confirmET.getText().toString())) {
					makeText(getApplicationContext(),
							R.string.retype_password_toast, LENGTH_LONG).show();
					return;
				}

				// Then check the email:
				String email = emailET.getText().toString().toLowerCase();

				if (!email.matches("[a-z\\d._%+-]+@[a-z\\d.-]+\\.[a-z]{2,4}")) {
					makeText(getApplicationContext(),
							R.string.email_format_toast, LENGTH_LONG).show();
					emailET.requestFocus();
					return;
				}

				// Now try to create the user and hope no exceptions pop up.
				if (lh.createUser(username, password, email)) {
					try {
						lh.saveDatabase();
						makeText(getApplicationContext(),
								R.string.new_account_toast, LENGTH_LONG).show();
						Intent i = new Intent(RegisterActivity.this
								.getApplicationContext(), MainActivity.class);
						startActivity(i);
						finish();
						finish();
					} catch (Exception e) {
						makeText(getApplicationContext(),
								R.string.database_save_failed, LENGTH_LONG)
								.show();
					}
				} else
					makeText(getApplicationContext(),
							R.string.registration_failed_toast, LENGTH_LONG)
							.show();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}